package com.steam.job;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.steam.entity.ItemsChuandi;
import com.steam.entity.ItemsChuandiExample;
import com.steam.entity.Phone;
import com.steam.mapper.ItemMapper;
import com.steam.mapper.ItemsChuandiMapper;
import com.steam.service.JedisClient;
import com.steam.util.GsonBuilderUtil;
//@Component
public class JinanSpringJob {
	@Autowired
	private RabbitTemplate rabbitTemplate;
	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private ItemMapper itemMapper;

	private static SqlSessionFactory sqlSessionFactory;
	Gson gson = GsonBuilderUtil.create();
	static {
		String resource = "mybatis/JinanSqlMap.xml";
		InputStream inputStream;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public boolean isContainChinese(String str) { // 判断字符串中是否含有中文
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher m = p.matcher(str);
		if (m.find()) {
			return true;
		}
		return false;
	}

	public boolean HasDigit(String content) { // 判断字符串中是否含有数字
		boolean flag = false;
		Pattern p = Pattern.compile(".*\\d+.*");
		Matcher m = p.matcher(content);
		if (m.matches()) {
			flag = true;
		}
		return flag;
	}
	// @Scheduled(cron = "0/1 * 6-23 * * ? ")
	@Scheduled(fixedDelay = 500)
	public void jining() {
		SqlSession sqlSession = null;
		// 数据库会话实例
		ItemsChuandiExample example = new ItemsChuandiExample();
		String jishu = jedisClient.get("ljinan");
		try {
			// 创建数据库会话实例sqlSession
			sqlSession = sqlSessionFactory.openSession();
			ItemsChuandiMapper mapper = sqlSession.getMapper(ItemsChuandiMapper.class);
			if (StringUtils.isNotBlank(jishu)) {
				example.createCriteria().andIdGreaterThan(Integer.parseInt(jishu));
			} else {
				// Integer selectMaxId = mapper.selectMaxId();
				// if(selectMaxId != null){
				// example.createCriteria().andMessageidGreaterThanOrEqualTo(selectMaxId);
				// }
			}
			PageHelper.startPage(1, 50, false);
			PageHelper.orderBy(" id ");
			List<ItemsChuandi> selectByExamples = mapper.selectByExample(example);
			if (selectByExamples != null && selectByExamples.size() > 0) {
				for (ItemsChuandi tblinfo : selectByExamples) {
					StringBuffer sb = new StringBuffer();
					List<Phone> phone_arr = new ArrayList<>();
					String o_phones = tblinfo.getUserPhones();
					String[] phoneList = o_phones.trim().split(" ");
					if (phoneList != null && phoneList.length > 0) {
						for (int i = 0; i < phoneList.length; i++) {
							// 如果得到的电话字段不是空并且不包含汉字并且都是数字
							if (StringUtils.isNotBlank(phoneList[i]) && !isContainChinese(phoneList[i])&& HasDigit(phoneList[i])) {
								if (phoneList[i].length() == 8) {
									sb.append("0531" + phoneList[i] + " ");
									Phone phone = new Phone();
									phone.setPhone("0531" + phoneList[i]);
									phone_arr.add(phone);// 消息队列当中传输的电话
								} else {
									sb.append(phoneList[i] + " ");
									Phone phone = new Phone();
									phone.setPhone(phoneList[i]);
									phone_arr.add(phone);
								}
							} else {
								sb.append(phoneList[i] + " ");
								Phone phone = new Phone();
								phone.setPhone(phoneList[i]);
								phone_arr.add(phone);
							}
						}
					}
					com.steam.entity.Item items = new com.steam.entity.Item();
					items.setCreateTime(tblinfo.getCreateTime());// 创建信息的时间
					items.setUserPhones(sb.toString().trim());// 电话信息
					items.setContent(tblinfo.getContent());// 信息的内容
					items.setCity(tblinfo.getCity());// 出发地 市
					items.setFormin("济南");// 数据源的来源
					items.setUsername(tblinfo.getUsername());// 济南导入的数据对应的用户编号
					// 推送给德州网平台bean
					Item item = new Item();
					item.setContent(tblinfo.getContent());
					// item.setPhones(sb.toString().trim());
					item.setTime(tblinfo.getCreateTime());
					User user = new User();
					user.setPhoneList(phone_arr);
					item.setUser(user);
					if (0 == tblinfo.getTypeId()) {// 目前济南的数据0是车源1是货源
						items.setTypeId(2);
						item.setTypeId(2);
						try {
							itemMapper.insertSelective(items);
						} catch (Exception e) {
							jedisClient.set("ljinan", tblinfo.getId().toString());
							e.printStackTrace();
						}
						rabbitTemplate.convertAndSend("*." + items.getFormin(), gson.toJson(item));
					} else {
						items.setTypeId(1);
						item.setTypeId(1);
						try {
							itemMapper.insertSelective(items);
						} catch (Exception e) {
							jedisClient.set("ljinan", tblinfo.getId().toString());
							e.printStackTrace();
						}
						rabbitTemplate.convertAndSend("*." + items.getFormin(), gson.toJson(item));
					}
					jedisClient.set("ljinan", tblinfo.getId().toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			jedisClient.set("ljinan", Integer.parseInt(jishu) + 1 + "");
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
	}
	public static void main(String[] args) {
		String string = "电信内线 68693";
		String reg = "[\u4e00-\u9fa5]";
		int index = -1;
		if (string.matches(".*" + reg + ".*")) {
			index = string.split(reg)[0].length();
		}
		System.out.println(index);
	}
}